package org.example.rabbitmq.service;

import org.example.rabbitmq.bean.Book;
import org.example.rabbitmq.config.MyAMQPConfig;
import org.example.rabbitmq.repository.BookRepository;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookService {
    @Autowired
    private BookRepository bookRepository;

    public Book saveBook(Book book) {
        return bookRepository.save(book);
    }

    @RabbitListener(queues = MyAMQPConfig.TEST_QUEUE_NAME)
    public void receive(Book book) {
        System.out.println("收到消息：" + book);
        saveBook(book);
    }
}